package com.test.springboot3.springboot3testdemo1.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.util.Objects;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/7/28-22:53
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    final HttpSession session = request.getSession();
    final String userSession = (String) session.getAttribute("userSession");
    if (Objects.isNull(userSession)) {
      response.sendRedirect("/login");
      return false;
    } else {
      return true;
    }
  }
}
